//index.js
//获取应用实例 
var app = getApp()
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    token: '',
    accountId: '',
    codeId: '',
    passId: '',
    paddPassId: '',
    inviteId: '',
    inviteCode: '',
    code: '',//验证码
    iscode: null,//用于存放验证码接口里获取到的code
    codename: '获取验证码',
    temp: ''
  },
  onShow: function () {

  },
  onLoad: function () {
    //加载本页面的tabBar样式
    var token = app.token;
    // 查看是否授权
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              // console.log(res.userInfo)
            }
          })
        }
      }
    })
  },
  accountInput: function (e) {
    var that = this;
    var phone = e.detail.value;
    if (!(/^1[34578]\d{9}$/.test(phone))) {

      this.setData({
        ajxtrue: false
      })
      if (phone.length >= 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'success',
          duration: 2000
        })
      }
    } else {
      this.setData({
        ajxtrue: true
      })
      that.setData({
        accountId: e.detail.value
      })
      console.log('验证成功', that.data.ajxtrue)
    }

  },
  codeInput: function (e) {
    var that = this;
    that.setData({
      code: e.detail.value
    })
  },
  passwordInput: function (e) {
    var that = this;
    that.setData({
      passId: e.detail.value
    })
  },
  passwordPassInput: function (e) {
    var that = this;
    that.setData({
      paddPassId: e.detail.value
    })
  },
  inviteInput: function (e) {
    var that = this;
    that.setData({
      inviteId: e.detail.value
    })
  },
  inviteCodeInput: function (e) {
    var that = this;
    that.setData({
      inviteCode: e.detail.value
    })
  },

  onReady: function () {
    wx.setNavigationBarTitle({
      title: '注册'
    })
  },

  //注册
  register: function () {
    var that = this;
    if (that.data.accountId.length == 0) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return;
    }
    if (that.data.code.length == 0) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      })
      return;
    }
    if (that.data.passId.length == 0) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      })
      return;
    }
    if (that.data.paddPassId.length == 0) {
      wx.showToast({
        title: '请输入确认密码',
        icon: 'none'
      })
      return;
    }
    if (that.data.inviteId.length == 0) {
      wx.showToast({
        title: '请输入邀请码',
        icon: 'none'
      })
      return;
    }
    var referrer_phone;
    console.log(that.data.inviteId)
    if (that.data.inviteId.length > 0) {
      if (that.data.inviteId.length == 11) {
        referrer_phone = 'referrer_phone';
        that.setData({
          referrer_phone: 'referrer_phone'
        })
      } else {
        referrer_phone = 'auth_code';
        that.setData({
          referrer_phone: 'auth_code'
        })
      }
    }
    console.log(referrer_phone);

    wx.request({
      url: app.globalData.api + '/index.php/wap/UserAccount/register',
      data:
      {
        phone: that.data.accountId,//手机号码
        pwd1: that.data.passId,//密码
        pwd2: that.data.paddPassId,//重复密码
        code: that.data.code,//手机短信验证码
        referrer_phone: that.data.inviteId,//推荐人手机号码/用户名
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.code == 1) {
          wx.showToast({
            title: '注册成功',//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'success', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })

          setTimeout(function () {
            //要延时执行的代码
            wx.redirectTo({
              url: 'login',
            })
          }, 1000) //延迟时间 这里是1秒

        } else {
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
      }, fail: function () {

      }
    });
  },

  getCode: function () {
    var a = this.data.accountId;
    var _this = this;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else if (!myreg.test(this.data.accountId)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 1000
      })
      return false;
    } else {
      console.log(this.data.accountId)
      wx.request({
        url: app.globalData.api + '/index.php/wap/Sms/sendUserRegister',
        data: {
          'phone': this.data.accountId,//手机号码
        },
        headers: {
          'platform': '' + app.globalData.platform,
          'version': '' + app.globalData.version,
        },
        method: 'POST',
        dataType: 'json',
        success(res) {
          console.log(res)
          if (res.data.code == 1) {
            _this.setData({
              iscode: res.data.data
            })
            var num = 61;
            var timer = setInterval(function () {
              num--;
              if (num <= 0) {
                clearInterval(timer);
                _this.setData({
                  codename: '重新发送',
                  disabled: false
                })

              } else {
                _this.setData({
                  codename: num + "s"
                })
              }
            }, 1000)

            wx.showToast({
              title: '' + res.data.msg,//提示文字
              duration: 2000,//显示时长
              mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
              icon: 'success', //图标，支持"success"、"loading"  
              success: function () { },//接口调用成功
              fail: function () { },  //接口调用失败的回调函数  
              complete: function () { } //接口调用结束的回调函数  
            })
          } else {
            wx.showToast({
              title: '' + res.data.msg,//提示文字
              duration: 2000,//显示时长
              mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
              icon: 'none', //图标，支持"success"、"loading"  
              success: function () { },//接口调用成功
              fail: function () { },  //接口调用失败的回调函数  
              complete: function () { } //接口调用结束的回调函数  
            })
          }


        }
      })

    }
  },
  //获取验证码
  getVerificationCode: function () {
    console.log("click");
    this.getCode();
    var _this = this
    _this.setData({
      disabled: true
    })
  },
})
